//
//  Token.m
//  CarTown
//
//  Created by 熊浩 on 15/9/1.
//  Copyright (c) 2015年 熊浩. All rights reserved.
//

#import "Token.h"
#import "AppDelegate.h"

@implementation Token

@dynamic token;
@dynamic time;
+(instancetype)TokenWithDict:(NSString *)msg{
    AppDelegate *delegate= [[UIApplication sharedApplication] delegate];
    NSFetchRequest *request=[NSFetchRequest fetchRequestWithEntityName:@"Token"];
    NSArray *sets= [delegate.managedObjectContext executeFetchRequest:request error:nil];
    if (sets.count>0) {
        for (Token *mtoken in sets) {
            [delegate.managedObjectContext deleteObject:mtoken];
        }
    }
    Token *token=[NSEntityDescription insertNewObjectForEntityForName:@"Token" inManagedObjectContext:delegate.managedObjectContext];
    token.token=msg;
    token.time=[NSDate date];
    [delegate saveContext];
    return token;
}

+(instancetype)getToken{
    AppDelegate *delegate= [[UIApplication sharedApplication] delegate];
    NSFetchRequest *request=[NSFetchRequest fetchRequestWithEntityName:@"Token"];
    NSArray *sets= [delegate.managedObjectContext executeFetchRequest:request error:nil];
    if (sets.count>0) {
        return sets[0];
    }else{
        return nil;
    }

}
@end
